package com.xhu.dao.impl;

import com.xhu.dao.CountDao;
import com.xhu.utils.ConnectionFactory;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

public class CountDaoImpl implements CountDao {
    @Override
    public Map<String, Object> countNumber() {
        Map<String, Object> map=new HashMap<>();
        Map<String, Object> map2=new HashMap<>();
        Map<String, Object> map3=new HashMap<>();
        String  sql1="select count(emp_id) as v1 from emp";
        String  sql2="select count(saleplan_id) as v2 from saleplan";
        String  sql3="select count(forecast_id) as v3 from forecast";
        //获得连接对象
        Connection connection= ConnectionFactory.openConnection();
        //执行对象
        QueryRunner queryRunner=new QueryRunner();
        try {
            map= queryRunner.query(connection, sql1, new MapHandler());
            map2= queryRunner.query(connection, sql2, new MapHandler());
            map3= queryRunner.query(connection, sql3, new MapHandler());
            map.put("v2",map2.get("v2"));
            map.put("v3",map3.get("v3"));
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            ConnectionFactory.closeConnection(connection);
        }
        return map;
    }

}
